<?php
/**
 *   File functions:
 *   Register new players
 *
 *   @name                 : register.php                            
 *   @copyright            : (C) 2005,2006 Vallheru Team based on Gamers-Fusion ver 2.5
 *   @author               : thindil <thindil@users.sourceforge.net>
 *   @version              : 1.0
 *   @since                : 04.02.2006
 *
 */

//
//
//       This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// $Id$

require 'libs/Smarty.class.php';
require_once ('includes/config.php');

$smarty = new Smarty;
$smarty -> compile_check = true;

require_once("languages/pl/register.php");

$objOpenreg = $db -> Execute("SELECT value FROM settings WHERE setting='register'");
/**
* When registration is closed
*
if ($objOpenreg -> fields['value'] == 'N') 
{
    $objReason = $db -> Execute("SELECT value FROM settings WHERE setting='close_register'");

        $smarty -> assign ("Error", 'Zaproszenia zamknięte tymczasowo z powodu: '.$objReason -> fields['value']);
        $smarty -> display ('error.tpl');
        exit;

    $objReason -> Close();
}
$objOpenreg -> Close();

$query = $db -> Execute("SELECT id FROM players");
$nump = $query -> RecordCount();
$query -> Close(); 
*/

$time = date("H:i:s");
$hour = explode(":", $time);
$newhour = $hour[0] + 0;
if ($newhour > 23) 
{
    $newhour = $newhour - 24;
}
$arrtime = array($newhour, $hour[1], $hour[2]);
$newtime = implode(":",$arrtime);
/*
$span = (time() - 180);
$objQuery = $db -> Execute("SELECT id FROM players WHERE lpv>=".$span);
$intNumo = $objQuery -> RecordCount();
$objQuery -> Close();
*/

if (isset ($_GET['action']) && $_GET['action'] == 'register') 
{
/**
* Check for empty fields
*/
    if (!$_POST['email']) 
    {
        $smarty -> assign ("Error", 'Puste pole!');
        $smarty -> display ('error.tpl');
        exit;
    }
    
/**
* Email adress validation
*/       
    require_once('includes/verifymail.php');
    if (MailVal($_POST['email'], 2)) 
    {
        $smarty -> assign ("Error", BAD_EMAIL);
        $smarty -> display ('error.tpl');
        exit;
    }
 
    $strEmail = $db -> qstr($_POST['email'], get_magic_quotes_gpc());
    $query = $db -> Execute("SELECT id FROM players WHERE email=".$strEmail);
    $dupe2 = $query -> RecordCount();
    $query -> Close();
    if ($dupe2 > 0) 
    {
        $smarty -> assign ("Error", EMAIL_HAVE);
        $smarty -> display ('error.tpl');
        exit;

    }
	if ($_POST['ref']){
		$ref = $_POST['ref'];
	} else $ref = 1;
    if (!ereg("^[1-9][0-9]*$", $ref)) 
    {
        $smarty -> assign ("Error", 'Niestety wystąpił błąd polecającego, spróbuj ponownie.');
        $smarty -> display ('error.tpl');
        exit;
    }
    $strEmail = strip_tags($_POST['email']);
    $strEmail = $db -> qstr($strEmail, get_magic_quotes_gpc());
//    $_POST['pass'] = strip_tags($_POST['pass']);
	$strPass = substr(md5(date("d.m.Y.H.i.s").rand(1,1000000)) , 0 , 5); 
    $aktw = rand(1,10000000);
	$time = date("H:i:s");
    $data = date("y-m-d");
	$arrtemp = array($data, $time);
	$newdate = implode(" ",$arrtemp);
    $strDate = $db -> DBDate($data);
    $ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
	$linkk = $gameadress."/aktywacja.php?kod=".$aktw." \n\nTwój email to:".$strEmail." Twoje hasło to: ".$strPass;
    $subject = "Rejestracja w grze ".$gamename;
	$autor = $adminmail;
    $message = "Kliknij w link aby aktywować swoje konto!\nLink: ".$linkk."\n\nCo to za gra? Jest to turowa, tekstowa gra RPG dla wielu graczy z elementami strategii i ekonomi. W świecie o realiach wczesnego średniowiecza z elementami magii poznasz wielu nowych znajomych, z którymi miło spędzisz czas.";
    $adress = $_POST['email'];
    require_once('mailer/mailerconfig.php');
    if (!$mail -> Send()) 
    {
        $smarty -> assign("Error", EMAIL_ERROR.$mail -> ErrorInfo);
        $smarty -> display('error.tpl');
        exit;
    }
    $strPass = MD5($strPass);
    $db -> Execute("INSERT INTO aktywacja (user, email, pass, refs, aktyw, data, ip, lang) VALUES('Nieznajomy', ".$strEmail.", '".$strPass."', ".$ref.", ".$aktw.", ".$strDate." , '".$ip."' ,'pl')") or die($db -> ErrorMsg());
    
        $smarty -> assign("Error", "Sprawdź pocztę ".$_POST['email'].", dostaniesz w mailu link <b>aktywacyjny oraz hasło</b> do konta! Jeśli w ciągu 5 minut wiadomość nie przyjdzie, spróbuj ponownie z inną pocztą (np. gmail)");
        $smarty -> display('error.tpl');
        exit;
}

/**
* Initialization of variable
*/
if (!isset($_GET['action'])) 
{
    $_GET['action'] = '';
}

/**
* Assign variables and display page
*/
$smarty -> assign(array("Action" => $_GET['action'], "Meta" => ''));
$smarty -> display('error.tpl');

$db -> Close();
?>
